package com.ruoyi.web.controller.car;


import com.ruoyi.car.business.domain.Statement;
import com.ruoyi.car.business.domain.StatementItem;
import com.ruoyi.car.business.query.StatementItemQuery;
import com.ruoyi.car.business.service.IStatementItemService;
import com.ruoyi.car.business.service.IStatementService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TablePageInfo;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("business/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "business/statementItem/";

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IStatementItemService statementItemService;

    @Autowired
    private IStatementService statementService;

    @RequestMapping("/itemDetail")
    public String itemDetail(Model model, Long statementId) {
        // 根据statementId查询结算单对象
        Statement statement = statementService.get(statementId);
        // 将数据传给前台
        model.addAttribute("statement", statement);
        statement.setPayee(userService.selectUserById(statement.getPayeeId()));
        if (Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            // 消费中的客户进入到明细录入页面
            return prefix + "itemEdit";
        } else {
            // 已支付用户跳转到明细详情页面
            return prefix + "itemDetail";
        }
    }

    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo query(StatementItemQuery qo) {
        return statementItemService.query(qo);
    }

    @ResponseBody
    @RequestMapping("/saveItems")
    public AjaxResult saveItems(@RequestBody List<StatementItem> items) {
        statementItemService.saveItems(items);
        return AjaxResult.success();
    }

    @ResponseBody
    @RequestMapping("/payStatement")
    public AjaxResult payStatement(Long statementId) {
        statementItemService.payStatement(statementId);
        return AjaxResult.success();
    }
}
